package com.yulian.util;

import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.Log4JLoggerFactory;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.auth.User;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.SqlConnection;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author:  
 * date: 2023/7/22 11:13
 * @since: JDK 1.8
 * @description: 数据库连接工具类
 */
public class CommonUtils {

    static final InternalLogger log = Log4JLoggerFactory.getInstance(CommonUtils.class);

    /**
     * 获取数据库连接
     *
     * @param client 连接池对象
     * @return 数据库连接
     */
    public static Future<SqlConnection> getPool(PgPool client) {
        Promise<SqlConnection> promise = Promise.promise();
        //从数据库连接池获取连接对象
        client.getConnection(ar1 -> {
            if (ar1.succeeded()) {
                SqlConnection conn = ar1.result();
                promise.complete(conn);
            } else {
                log.info("连接数据库错误信息: " + ar1.cause().toString());
                //这里就相当于继续向上抛出异常，用Promise来向上抛异常
                promise.fail(ar1.cause());
            }
        });
        return promise.future();
    } 

    /**
     * 创建返回值正常的Json对象
     *
     * @param data 数据
     * @return JsonObject
     */
    public static JsonObject createJsonObject(Object data) {
        JsonObject responseJSObj = new JsonObject();
        responseJSObj.put("code", 200);
        responseJSObj.put("msg", "请求成功");
        responseJSObj.put("data", data);
        return responseJSObj;
    }
    /**
     * 创建返回值异常Json对象
     *
     * @param re  异常对象
     * @param url 路径
     * @param msg 异常信息
     * @return JsonObject
     */
    public static JsonObject createExceptionJsonObject(Object re, String url,String msg) {
        JsonObject responseJSObj = new JsonObject();
        log.warn("[结果异常] 结果异常:{}，方法路径：{}", re, url);
        responseJSObj.put("code", -1);
        responseJSObj.put("msg", msg);
        return responseJSObj;
    }
    /**
     * 创建返回值异常的Json对象字符串
     *
     * @param msg 异常信息
     * @return JsonObject
     */
    public static String createExceptionJsonObjectStr(Object re, String url, String msg) {
        JsonObject responseJSObj = new JsonObject(); 
        log.warn("[结果异常] 结果异常:{}，方法路径：{}", re, url);
        responseJSObj.put("code", -1);
        responseJSObj.put("msg", msg); 
        return responseJSObj.toString();
    }
    /** 重载1
     * 创建返回值异常的Json对象字符串
     *
     * @param msg 异常信息
     * @return JsonObject
     */
    public static String createExceptionJsonObjectStr(Object re, String url, String msg, String sql) {
        JsonObject responseJSObj = new JsonObject();
        log.error("[结果异常] 结果异常:{},方法路径：{}, sql：{}", re, url,sql);
        responseJSObj.put("code", 500);
        responseJSObj.put("msg", msg);
        return responseJSObj.toString();
    }
    
    /** 重载2
     * 创建返回值异常的Json对象字符串
     *
     * @param msg 异常信息
     * @return JsonObject
     */
    public static String createExceptionJsonObjectStr(Integer code,String msg) {
        JsonObject responseJSObj = new JsonObject();
        responseJSObj.put("code", code);
        responseJSObj.put("msg", msg);
        return responseJSObj.toString();
    } 
    
    public static String createExceptionJsonObjectStr(Integer code,String url, String msg) {
        JsonObject responseJSObj = new JsonObject();
        log.error("方法路径：{}, msg：{}",   url,msg);
        responseJSObj.put("code", code);
        responseJSObj.put("msg", msg);
        return responseJSObj.toString();
    }  
     
    /**  
     * 创建返回值异常的字符串
     *
     * @param re  异常对象
     * @param url 路径
     * @param msg 异常信息
     * @return JsonObject
     */
    public static String createJsonObject(Object re, String url,String msg) {
        JsonObject responseJSObj = new JsonObject();
        log.warn("[结果异常] 结果异常:{}，方法路径：{}", re, url);
        responseJSObj.put("code", -1);
        responseJSObj.put("msg", msg);
        return responseJSObj.toString();
    }  

    /**
     * 创建返回值异常的字符串
     *
     * @param msg 异常信息
     * @return JsonObject
     */
    public static String createJsonObject(Integer code,String msg) {
        JsonObject responseJSObj = new JsonObject();
        responseJSObj.put("code", code);
        responseJSObj.put("msg", msg);
        return responseJSObj.toString();
    }

    /**
     * 功能模块生成单据号
     */

    /**
     * 生成单据号
     *
     * @param type           类型
     * @param siteCode       地区code
     * @param gatherAreaCode 商品聚集区code
     * @param orderNumber    自然数
     * @return 单据号
     */
    public static String generateOrderNumber(String type, String siteCode, String gatherAreaCode, Integer orderNumber) {
        StringBuilder string = new StringBuilder();
        string.append(type).append("-");
        string.append(siteCode).append("-");
        string.append(gatherAreaCode).append("-");
        LocalDateTime currentLocalDateTime = TimeUtils.getCurrentLocalDateTime();
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "YYYY").substring(2));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "MM"));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "dd"));
        string.append(orderNumber);
        return string.toString();
    }
    
    /**
     * 生成销售地区供应链商品聚集区预提计划单编号
     *
     * @param siteCodePinYin       地区code拼音
     * @param gatherAreaCodePinYin 商品聚集区code拼音
     * @param orderNumber    自然数
     * @return 单据号
     */
    public static String generatePickupPlanCode(String siteCodePinYin, String gatherAreaCodePinYin, Integer orderNumber) {
        StringBuilder string = new StringBuilder();
        string.append("SYT").append("-");
        string.append(siteCodePinYin).append("-");
        string.append(gatherAreaCodePinYin).append("-");
        LocalDateTime currentLocalDateTime = TimeUtils.getCurrentLocalDateTime();
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "YYYY").substring(2));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "MM"));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "dd"));
        string.append(orderNumber);
        return string.toString();
    }

    /**
     * 生成支付单编号
     *
     * @param type       类型
     * @param supplyChainCode 供应商pinyin
     * @param pickupPlanCodeParam   计划单code
     * @return 单据号
     */
    public static String generatePickupPlanPaymentCode(String type, String supplyChainCode, String pickupPlanCodeParam) {
        StringBuilder string = new StringBuilder();
        string.append(type).append("-");
        string.append(supplyChainCode).append("-");
        LocalDateTime currentLocalDateTime = TimeUtils.getCurrentLocalDateTime();
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "YYYY").substring(2));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "MM"));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "dd"));
        string.append("-");
        string.append(pickupPlanCodeParam);
        return string.toString();
    }
    
    public static String generatePreparePickupPlanNumber(String type,String pinyin, Integer orderNumber) {
        StringBuilder string = new StringBuilder();
        string.append(type).append("-");
        string.append(pinyin).append("-");
        LocalDateTime currentLocalDateTime = TimeUtils.getCurrentLocalDateTime();
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "YYYY").substring(2));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "MM"));
        string.append(TimeUtils.dateTimeFormatterString(currentLocalDateTime, "dd"));
        string.append(orderNumber);
        return string.toString();
    }

    /**
     * 生成销售地区供应链预提计划平衡单编号
     *
     * @param pickupPlanCode       销售地预提计划单编号
     * @return 单据号
     */
    public static String generatePickupPlanBalanceCode(String type,String pickupPlanCode) {
        StringBuilder string = new StringBuilder();
        string.append(type).append("-");
        TimeUtils.currentDay(string).append("-").append(pickupPlanCode);
        return string.toString();
    }


    /**
     * 获取用户id
     * @param user user
     * @return 用户id
     */
    public static String getUserId(User user){
        return  user.attributes().getJsonObject("accessToken").getInteger("userId").toString();
    }


    /**
     * 拼接sql
     * @param list list
     * @return 字符串
     */
    public static String inSql(List<String> list){
        return  "'" + String.join("','", list) + "'";
    }

    public static Object generateMainlineFreightPaymentCode(String type, String goodsGatherAreaPinyiin, String sitePinyiin, String truckTypePinyiin, String number) {

        return null;
    }
}
